package com.zsy.service.Impl;

import com.zsy.dao.UserDao;
import com.zsy.pojo.User;
import com.zsy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserDao userDao;

    /**
     * 登录验证
     *
     * @param phone         手机号
     * @param code          用户输入验证码
     * @param codeInSession 系统生成验证码
     * @return 一个用户实体类 判断是否为空
     */

    @Override
    public User login(String phone, String code, String codeInSession) {
        //校验验证码是否一致
        User dbuser = null;
        if (code.equalsIgnoreCase(codeInSession)) {
            User user =userDao.selectByPhone(phone);
            //判断是否新用户
            if (user==null) {
                dbuser = new User();
                dbuser.setPhone(phone);
                dbuser.setStatus(1);
                userDao.save(dbuser);
            }else {
                return user;
            }
        }
        return dbuser;
    }
}

